package com.ffyc.resale.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ffyc.resale.model.*;
import com.ffyc.resale.service.AdminService;
import com.ffyc.resale.service.GoodsService;
import com.ffyc.resale.service.UserService;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping
@Api(tags = "登录注册页面")
public class WebController {
    @Autowired
    AdminService adminService;
    @Autowired
    UserService userService;
    @Autowired
    GoodsService goodsService;

    /**
     * 登录
     * */
    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        if (account.getEmail().isEmpty() || account.getPassword().isEmpty()
                || account.getRole().isEmpty()) {
            Result res =new Result(500,"网络异常",null);
            return res;
        }
        account = adminService.login(account);
        if (account==null){
            Result res =new Result(201,"账号或密码错误",null);
            return res;
        }
        Result result = new Result(200,"登录成功",account);
        return result;
    }
    @RequestMapping("/CreateYZM")
    public Result CreateYZM(String email){
        Result result=userService.CheckAccont(email);
        return result;
    }
    @PostMapping("/register")
    public Result addUser(@RequestBody User user){
         userService.addUser(user);
         return new Result(200,"成功",true);
    }
    //二手交易试产商品信息
    @PostMapping("/findGoods")
    public Result findGoodsList(@RequestBody GoodsSearch goods){
        Page<Goods> list = goodsService.findGoods(goods);
        return new Result(200,"成功",list);
    }
    //查询单个信息详细
    @GetMapping("/findGoodById")
    public Result findGoodById( @RequestParam Integer id){
        System.out.println(id);
       Goods goods= goodsService.findGoodsById(id);
        return new Result(200,"成功",goods);
    }
    //通过类型查询
    @PostMapping("/findGoodsByType")
    public Result findGoodsByType(@RequestBody GoodsSearch goodsSearch){
        Page<Goods> list = goodsService.findGoodsByType(goodsSearch);
        System.out.println(goodsSearch.getTypesid());
        return new Result(200,"成功",list);
    }
}
